package com.qianyu.project.web.controller.wyh;

import com.qianyu.project.entity.LaborSubcontract;
import com.qianyu.project.entity.PaymentList;
import com.qianyu.project.entity.QuantitiesList;
import com.qianyu.project.entity.SubcontractProgressPayment;
import com.qianyu.project.service.IPaymentListService;
import com.qianyu.project.service.ISubcontractProgressPaymentService;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * author wyh
 * Date 2021/2/8
 **/
@RestController
@RequestMapping("WyhSubcontractProgressPayment")
public class SubcontractProgressPaymentController {

    @Autowired
    private ISubcontractProgressPaymentService iSubcontractProgressPaymentService;

    @Autowired
    private IPaymentListService iPaymentListService;

    @GetMapping("/code")
    public Result getMaxCode() {
        return ResultTool.success(Long.parseLong(iSubcontractProgressPaymentService.getMaxCode()) + 1);
    }

    @PostMapping("")
    public Result addLaborSubcontract(@RequestBody Map<String,Object> adds){
        System.out.println(adds);
        SubcontractProgressPayment spp = new SubcontractProgressPayment();
        Map<String,Object> mapls = (Map<String, Object>) adds.get("subcontracts");
        spp.setPId(Integer.valueOf(mapls.get("pId")+""));
        spp.setSbid(Integer.valueOf(mapls.get("sbid")+""));
        spp.setConId(Integer.valueOf(mapls.get("conId")+""));
        spp.setProId(Integer.valueOf(mapls.get("proId")+""));
        spp.setSbname(mapls.get("sbname")+"");
        spp.setContractAmount(BigDecimal.valueOf(Double.valueOf(mapls.get("contractAmount")+"")));
        spp.setSettleType(mapls.get("settleType")+"");
        spp.setAgreeAmount(BigDecimal.valueOf(Double.valueOf(mapls.get("agreeAmount")+"")));
        spp.setAgreeUpper(mapls.get("agreeUpper")+"");
        spp.setPenalty(BigDecimal.valueOf(Double.valueOf(mapls.get("penalty")+"")));
        spp.setWithhold(BigDecimal.valueOf(Double.valueOf(mapls.get("withhold")+"")));
        spp.setSettleComment(mapls.get("settleComment")+"");
        spp.setEId(Integer.valueOf(mapls.get("eId")+""));
        spp.setApplyDate(mapls.get("applyDate")+"");
        spp.setAttachIds(mapls.get("attachIds")+"");
        spp.setApproveStatus(Integer.valueOf(1+""));
        spp.setTypeId(Integer.valueOf(2+""));
        iSubcontractProgressPaymentService.saveOrUpdate(spp);
        List<Map<String,Object>> maptpd = (List<Map<String, Object>>) adds.get("tenderPurchaseDetails");
        List<PaymentList> aiList = Lists.newArrayList();
        maptpd.forEach(i->{
            PaymentList qq = new PaymentList();
            qq.setSubName(i.get("subList")+"");
            qq.setUnit(i.get("unit")+"");
            qq.setWorkCount(Integer.valueOf(i.get("workCount")+""));
            qq.setComperPrice(BigDecimal.valueOf(Double.valueOf(20+"")));
            qq.setSumPrice(BigDecimal.valueOf(Double.valueOf(i.get("sumPrice")+"")));
            qq.setSbCount(Integer.valueOf(i.get("sbCount")+""));
            qq.setHzCount(Integer.valueOf(i.get("hzCount")+""));
            qq.setSubId(spp.getId());
            aiList.add(qq);
        });
        iPaymentListService.saveBatch(aiList);
        return ResultTool.success();
    }

    @GetMapping("/list")
    public Result getSubcontracts(@RequestParam Map<String,Object> map){

        return ResultTool.success(iSubcontractProgressPaymentService.getSubcontracts(map));
    }
}
